#! /usr/bin/python
# -*- coding: UTF-8 -*-

import matplotlib.pyplot as plt
from pickle import load,dump

def draw_deg_prob():
    f = open('newrank.txt')
    lines = f.readlines()
    f.close()
    res = [0]*11000
    n = len(lines)

    for i in lines:
        deg = int(i.split(' : ')[1])
        res[deg] = res[deg] + 1
    all = sum([i**(-1.4) for i in xrange(1,6000)])

    data0 = [i/float(n) for i in res[1:8000]]
    data2 = [i**(-1.2)/all for i in xrange(1,6000)]
    plt.title('Degree Distribution of Random Network')
    plt.xlabel('Degree')
    plt.ylabel('Probability')
    plt.loglog(data0,'r-')
    plt.loglog(data2,'b--')
    plt.grid(True)
    plt.show()

def draw_ints_prob():
    res = [0] * 40000
    #f = load(open('interest_dict.pkl'))
    f = load(open('subject_dict.pkl'))
    all = f.items()
    for i in all:
        deg = i[1]
        try:
            res[deg] = res[deg] + 1
        except:
            print i

    data0 = [i/float(len(all)) for i in res]
    #plt.title('Interests Distribution')
    plt.title('Distribution of Collect Numbers of Subjects')
    plt.xlabel('Freq')
    plt.ylabel('Probability')
    plt.loglog(data0,'ro')
    plt.grid(True)
    plt.show()

def draw_freq_prob():
    res = [0] * 31
    f = load(open('user_freq.pkl'))
    all = f[0].items()
    for i in all:
        deg = i[1]
        res[deg] = res[deg] + 1

    data0 = [float(len(all))/i for i in res[1:31]]
    plt.title('Freq Distribution')
    plt.xlabel('Freq')
    plt.ylabel('Probability')
    plt.plot(data0,'ro-')
    plt.grid(True)
    plt.show()

def draw_bc_prob():
    f = open('out.txt')
    lines = f.readlines()
    f.close()
    res = [0]*6000000
    n = len(lines)

    for i in lines:
        deg = int(i.split(':')[1][:-1].split('.')[0])
        res[deg/100] = res[deg/100] + 1

    data0 = [i/float(n) for i in res[1:50000]]
    data1 = [i**(-2.2) for i in xrange(1,400)]
    data2 = [i**(-2) for i in xrange(1,400)]
    plt.title('BC Distribution')
    plt.xlabel('BC')
    plt.ylabel('Probability')
    plt.loglog(data0,'ro',data1,'r--',data2,'b--')
    plt.grid(True)
    plt.show()

if __name__ == '__main__':
    #draw_bc_prob()
    #draw_freq_prob()
    #draw_deg_prob()
    draw_ints_prob()
